package com.jwater.core.manager.common;

import com.jwater.core.manager.common.model.FileInfo;
import jwater.org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: jnner
 * @createDate: 2021/3/13
 * @version: 1.0
 */
public class FileInfoUtils {
    protected static Logger logger = LoggerFactory.getLogger(FileInfoUtils.class);

    public static List<FileInfo> listFileInfos(String dstDir) {
        List<FileInfo> list = new ArrayList<>();
        try {
            if (dstDir.endsWith(":")) {
                dstDir = dstDir + "/";
            }
            File dir = new File(dstDir);
            if (dir.exists()) {
                File[] files = dir.listFiles();
                if (files != null) {
                    for (File file : files) {
                        FileInfo info = new FileInfo();
                        info.setPath(file.getPath().replace("\\", "/"));
                        info.setName(file.getName());
                        if ("$RECYCLE.BIN".equals(file.getName()) || "System Volume Information".equals(file.getName())) {
                            continue;
                        }
                        if (file.isDirectory()) {
                            info.setType("1");
                        } else {
                            if (file.getAbsolutePath().equals(file.getCanonicalPath())) {
                                info.setType("2");
                            } else {
                                info.setType("3");
                            }
                        }
                        info.setSize(file.length());
                        info.setUpdateTime(file.lastModified());
                        list.add(info);
                    }
                }
            }
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
        return list;
    }
}
